from odoo import api, models, fields


class MrpBomLine(models.Model):
    _inherit = 'mrp.bom.line'

    def _compute_s_operation_id(self):
        for record in self:
            categ_id = record.product_id.categ_id
            select_ = """SELECT mrp_workcenter_id FROM s_mrp_workcenter_product_category"""
            if not categ_id.parent_id:
                where_ = f""" WHERE product_category_id={categ_id.id}"""
            else:
                where_ = f""" WHERE product_category_id in {(categ_id.id, categ_id.parent_id.id)}"""
            sql = select_ + where_
            self.env.cr.execute(sql)
            data = self.env.cr.fetchall()
            if data:
                record.s_operation_id = data[0][0]
            else:
                record.s_operation_id = None

    s_operation_id = fields.Many2one('mrp.workcenter', string='消耗工序', compute='_compute_s_operation_id')
